from Tkinter import *
from twisted.internet import interfaces, reactor
from twisted.internet.protocol import ServerFactory, Factory, ClientFactory, ReconnectingClientFactory
from twisted.protocols.basic import LineReceiver
from twisted.internet.protocol import Protocol
from time import time
import os, sys
import ImageTk, Image
from twisted.internet import tksupport
from twisted.protocols.basic import LineReceiver
from twisted.internet import interfaces, reactor

root = Tk()
tksupport.install(root)
label_text = ['Name', 'Down Rate', 'Up Rate', 'Parent', 'Child', '   ']
j = 0
for i in label_text:
    label = Label(text = i, relief=RIDGE, width = 15).grid(row=0, column=j)
    j = j+1
    
class Window:
    def __init__(self, protocol):
        self.protocol = protocol
        
    def addLabel(self, text, noOfRow):
        self.splittedText = text.split("@@")
        cols = 0
        for label_text in self.splittedText:
            self.label = Label(text=label_text, relief=SUNKEN, width=15).grid(row=noOfRow, column = cols)
            cols = cols+1
            
        self.button = Button(text='Refresh', relief=RAISED, width=10, command=self.button_clicked)
        self.button.grid(row=noOfRow, column = cols)
                
    def button_clicked(self):
        self.protocol.transport.write("get\r\n")

class ClientInfo:
    
    def getName(self):
        return self.name

    def setName(self, name):
        self.name = name

    def setRow(self, row):
        self.row = row

    def getRow(self):
        return self.row
    
def getRowVal(clientName):
    for val in factory.clients:
        if val.getName() == clientName:
            return val.getRow()
    return 0

class UIProtocol(LineReceiver):
    def connectionMade(self):
        print "connection made"
        
    def lineReceived(self, line):
        self.partitioned = line.split("@@")
        if line.startswith("uconnect:"):
            clientInfo = ClientInfo()
            clientInfo.setName(self.partitioned[0][9:])
            if len(factory.clients) == 0:
                clientInfo.setRow(0)
            clientInfo.setRow(len(factory.clients) + 1)
            factory.clients.append(clientInfo)
            Window(self).addLabel(line[9:], clientInfo.getRow())
            print line    
        elif line.startswith("data:"):
            self.clientRow = getRowVal(self.partitioned[0][5:])
            Window(self).addLabel(line[5:], self.clientRow)
        else:
            pass
            
    def connectionLost(self, reason):
        print reason

class UIFactory(ServerFactory):
    protocol = UIProtocol
    
    def clientConnectionFailed(self, connector, reason):
        print 'connection failed:', reason.getErrorMessage()
        reactor.stop()

    def clientConnectionLost(self, connector, reason):
        print 'connection lost:', reason.getErrorMessage()
        reactor.stop()

port = 3000
factory = UIFactory()
factory.clients = []
reactor.listenTCP(3000, factory)
reactor.run()
root.mainloop(0)
            